package com.lin.cloud.disk.exec.impl;

import com.lin.cloud.disk.exec.IExecCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author Lin
 */
@Component
@Slf4j
public class ExecCommandImpl implements IExecCommand {

    private Runtime runtime = Runtime.getRuntime();

    private ExecutorService executorService = new ThreadPoolExecutor(
            2,
            2,
            10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(2));
    @Override
    public void execCommand(String cmd) {
        try{
            executorService.submit(()->{
                try {
                    runtime.exec(cmd,null,null);
                    log.info("执行命令成功： " + cmd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
